package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.UmsRoleDao;
import com.macro.mall.dao.UmsRoleMenuDao;
import com.macro.mall.dao.UmsRoleResourceDao;
import com.macro.mall.service.UmsCacheService;
import com.macro.mall.service.UmsRoleService;
import com.macro.mapper.UmsRoleMapper;
import com.macro.mapper.UmsRoleMenuRelationMapper;
import com.macro.mapper.UmsRoleResourceRelationMapper;
import com.macro.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class UmsRoleServiceImpl implements UmsRoleService {

    @Autowired
    private UmsRoleMapper roleMapper;

    @Autowired
    private UmsRoleMenuRelationMapper roleMenuMapper;

    @Autowired
    private UmsRoleResourceRelationMapper roleResourceMapper;

    @Autowired
    private UmsRoleDao roleDao;

    @Autowired
    private UmsRoleMenuDao roleMenuDao;

    @Autowired
    private UmsRoleResourceDao roleResourceDao;

    @Autowired
    private UmsCacheService adminCacheService;

    @Override
    public int create(UmsRole role) {
        role.setCreateTime(new Date());
        role.setAdminCount(0);
        role.setSort(0);
        return roleMapper.insert(role);
    }

    @Override
    public int update(Long roleId, UmsRole role) {
       role.setId(roleId);
       return roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public int delete(List<Long> ids) {
        UmsRoleExample example = new UmsRoleExample();
        example.createCriteria().andIdIn(ids);
        adminCacheService.delResourceListByRoleIds(ids);
        return roleMapper.deleteByExample(example);
    }

    @Override
    public List<UmsPermission> getPermissionList(Long roleId) {
        return roleDao.getPermissionList(roleId);
    }

    @Override
    public int updatePermission(Long roleId, List<Long> permissions) {
        roleMapper.deleteByPrimaryKey(roleId);
        return roleDao.updatePermission(roleId,permissions);
    }

    @Override
    public List<UmsRole> list() {
        return roleMapper.selectByExample(new UmsRoleExample());
    }

    @Override
    public List<UmsRole> list(String keyword, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        UmsRoleExample example = new UmsRoleExample();
        if(!StringUtils.isEmpty(keyword))
            example.createCriteria().andNameLike("%" + keyword + "%");
        return roleMapper.selectByExample(example);
    }

    @Override
    public List<UmsMenu> getMenuList(Long adminId) {
        return roleDao.getMenuList(adminId);
    }

    @Override
    public List<UmsMenu> listMenu(Long roleId) {
        return roleDao.listMenu(roleId);
    }

    @Override
    public List<UmsResource> listResource(Long roleId) {
        return roleDao.listResource(roleId);
    }

    @Override
    public int allowMenu(Long roleId, List<Long> menuList) {
        roleMenuMapper.deleteByPrimaryKey(roleId);
        return roleMenuDao.insert(roleId,menuList);
    }

    @Override
    public int allowResource(Long roleId, List<Long> resourceList) {
        roleResourceMapper.deleteByPrimaryKey(roleId);
        adminCacheService.delResourceListByRole(roleId);
        return roleResourceDao.insert(roleId,resourceList);
    }
}
